<!DOCTYPE html>
<html lang="zh-CN" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>menu</title>
    <link rel="stylesheet" th:href="@{/lib/layui-v2.6.5/css/layui.css}" media="all">
    <link rel="stylesheet" th:href="@{/css/public.css}" media="all">
    <style>
        .layui-btn:not(.layui-btn-lg ):not(.layui-btn-sm):not(.layui-btn-xs) {
            height: 34px;
            line-height: 34px;
            padding: 0 8px;
        }
    </style>
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">
        <div>
            <div class="layui-btn-group">
                <button class="layui-btn" id="btn-expand">全部展开</button>
                <button class="layui-btn layui-btn-normal" id="btn-fold">全部折叠</button>
            </div>
            <table id="menu-table" class="layui-table" lay-filter="menu-table"></table>
        </div>
    </div>
</div>
<script type="text/html" id="page-toolbar">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add">添加</button>
        <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="delete">删除</button>
    </div>
</script>
<!-- 操作列 -->
<script type="text/html" id="auth-state">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<script th:src="@{/lib/layui-v2.6.5/layui.js}" charset="utf-8"></script>
<script th:src="@{/js/lay-config.js}" charset="utf-8"></script>
<script>
    layui.use(['table', 'treetable'], function () {
        var $ = layui.jquery;
        var table = layui.table;
        var treetable = layui.treetable;

        let helper = {
            ajaxDeleteByIds: function (ids) {
                $.ajax({
                    type: "GET",
                    url: "/sys-menu-api/delete",
                    data: {ids: ids},
                    dataType: "json",
                    success: function (data) {
                        if (data.code === 0) {
                            layer.msg('删除成功')
                            layui.table.reload('menu-table', {
                                url: '/sys-menu-api/menu'
                            });
                        } else {
                            layer.open({
                                title: 'error',
                                content: data
                            })
                        }
                    },
                    error: function (obj, data, err) {
                        layer.open({
                            title: 'error',
                            content: data.msg
                        })
                    }
                })
            }
        }

        // 渲染表格
        layer.load(2);
        treetable.render({
            treeColIndex: 1,
            treeSpid: 0,
            treeIdName: 'id',
            treePidName: 'pid',
            toolbar: '#page-toolbar',
            elem: '#menu-table',
            url: '/sys-menu-api/menu',
            page: false,
            cols: [
                [
                    {type: 'numbers'},
                    {field: 'title', minWidth: 200, title: '权限名称'},
                    {field: 'perms', title: '权限标识'},
                    {field: 'href', title: '菜单url'},
                    {field: 'sort', width: 80, align: 'center', title: '排序号'},
                    // {
                    //     field: 'isMenu',
                    //     width: 80,
                    //     align: 'center',
                    //     templet: function (d) {
                    //         if (d.isMenu == 1) {
                    //             return '<span class="layui-badge layui-bg-gray">按钮</span>';
                    //         }
                    //         if (d.parentId == -1) {
                    //             return '<span class="layui-badge layui-bg-blue">目录</span>';
                    //         } else {
                    //             return '<span class="layui-badge-rim">菜单</span>';
                    //         }
                    //     },
                    //     title: '类型'
                    // },
                    {
                        templet: '#auth-state',
                        width: 120,
                        align: 'center',
                        title: '操作'
                    }
                ]
            ],
            done: function () {
                layer.closeAll('loading');
            }
        });

        $('#btn-expand').click(function () {
            treetable.expandAll('#menu-table');
        });

        $('#btn-fold').click(function () {
            treetable.foldAll('#menu-table');
        });

        /**
         * toolbar监听事件
         */
        table.on('toolbar(menu-table)', function (obj) {
            if (obj.event === 'add') {  // 监听添加操作
                let index = layer.open({
                    title: '添加用户',
                    type: 2,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: ['100%', '100%'],
                    content: '/sys-menu-page/add.html',
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
            } else if (obj.event === 'delete') {  // 监听删除操作
                let checkStatus = table.checkStatus(obj.config.id)
                    , data = checkStatus.data;
                if (data.length == 0) {
                    layer.msg('请选择数据', {icon: 5});
                } else {
                    layer.confirm('确定删除' + data.length + '个用户', function (index) {
                        let ids = '';
                        for (let i = 0; i < data.length; i++) {
                            if (ids) {
                                ids += ',' + data[i]['id'];
                            } else {
                                ids = data[i]['id'];
                            }
                        }
                        helper.ajaxDeleteByIds(ids)
                        layer.close(index);
                    });
                }
            }
        });

        //监听工具条
        table.on('tool(menu-table)', function (obj) {
            var data = obj.data;
            var layEvent = obj.event;

            if (layEvent === 'del') {
                layer.confirm('确定删除菜单：' + obj.data.title, function (index) {
                    helper.ajaxDeleteByIds(obj.data.id)
                    layer.close(index);
                });
            } else if (layEvent === 'edit') {
                let index = layer.open({
                    title: '编辑菜单',
                    type: 2,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: ['100%', '100%'],
                    content: '/sys-menu-page/edit.html/' + data.id,
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
                return false;
            }
        });
    });
</script>
</body>
</html>